#coding:utf-8

import base
from base import CUSTOM
import time

#0101增加监控
class Buss0101(base.Buss):
    name = u"增加监控"
    busstype = [0x01, 0x01]
    dgu = {u'定时更新':0x0, 
           u'定距更新':0x1, 
           u'定时定距':0x2,
           CUSTOM:0x3}
                            
    dgs = {u'压缩数据一':0x0,
           u'压缩数据二':0x1, 
           u'标准数据':0x2 ,
           u'差分数据':0x3,
           CUSTOM:0x7}
        
    def init(self):
        self.gpsUpdate = 0x00
        self.gpsStyle = 0x02
        self.period = [0x00, 0x00]
        self.interval = [0x00, 0x00]
        self.distance = [0x00, 0x00]
        
    def separate(self, data):
        p = base.pickUp(data.get(1))
        self.gpsUpdate = p.get(3,4)
        self.gpsStyle = p.get(5,7)
        self.period = data.get(2)
        self.interval = data.get(2)
        self.distance = data.get(2)
        
    def assemble(self, data):
        p = base.pickUp()
        p.set(self.gpsUpdate, 3, 4)
        p.set(self.gpsStyle, 5, 7)
        data.set(p.out(), self.period, self.interval, self.distance)
        
        
    def getGpsUpdate(self):
        return base.getKey(self.dgu, self.gpsUpdate)
        
    def setGpsUpdate(self, value):
        self.gpsUpdate = self.dgu[value]
        
    def getGpsStyle(self):
        return base.getKey(self.dgs, self.gpsStyle)
            
    def setGpsStyle(self, value):
        self.gpsStyle = self.dgs[value]
        
    def getMonitorPeriod(self):
        return self.period[0] * 60 + self.period[1]
    
    def setMonitorPeriod(self, value):
        value = int(value)
        self.period[0] = value % 60
        self.period[1] = value / 60
        
    def getMonitorInterval(self):
        return self.interval[0] * 60 + self.interval[1]
    
    def setMonitorInterval(self, value):
        value = int(value)
        self.interval[0] = value % 60
        self.interval[1] = value / 60
        
    def getMonitorDistance(self):
        return self.distance[0] * 100 + self.distance[1]
    
    def setMonitorDistance(self, value):
        value = int(value)
        self.distance[0] = value % 100
        self.distance[1] = value / 100
        
    def getList(self):
        return [
                [u'GPS更新', self.dgu.keys(), self.getGpsUpdate, self.setGpsUpdate],
                [u'GPS类型', self.dgs.keys(), self.getGpsStyle, self.setGpsStyle],
                [u'监控时间/分', (0,100), self.getMonitorPeriod, self.setMonitorPeriod],
                [u'监控周期/秒', (0,60), self.getMonitorInterval, self.setMonitorInterval],
                [u'定距距离', (0,33), self.getMonitorDistance, self.setMonitorDistance]
                ]
        
    def play(self, hunter):
        hunter.sendData(self)
        hunter.requestMonitor(self.getMonitorPeriod() * 60)
        

#0102撤销监控
class Buss0102(base.Buss):
    name = u"撤销监控"
    busstype = [0x01, 0x02]
    
    dr = {u'正常撤销': 0x01,
            CUSTOM: 0x02}
    
    def init(self):
        self.reason = 0x00
        
    def separate(self, data):
        self.reason = data.get(1)
        
    def assemble(self, data):
        data.set(self.reason)
        
    def getReason(self):
        return base.getKey(self.dr, self.reason)
        
    def setReason(self, value):
        self.reason = self.dr[value]
        
    def getList(self):
        return [
                [u'撤销原因', self.dr.keys(), self.getReason, self.setReason]
                ]
        
    def play(self, hunter):
        hunter.sendData(self)
        hunter.requestStopMonitor()
        

#修改监控
class Buss0103(Buss0101):
    busstype = [0x01, 0x03]
    name = u"修改监控"

    def init(self):
        self.tel = [0x20] * 15
        Buss0101.init(self)
        
    def separate(self, data):
        self.tel = data.get(15)
        Buss0101.separate(self, data)
        
    def assemble(self, data):
        data.set(self.tel)
        Buss0101.assemble(self, data)
        
    def getTel(self):
        return base.data2unicode(self.tel)
    
    def setTel(self, value):
        tel = base.unicode2data(value)
        self.tel = tel + (15 - len(tel)) *[0x20]

    def getList(self):
        l = [
             [u'原中心手机号', 15, self.getTel, self.setTel],
             ]
        return l + Buss0101.getList(self)
        
    def play(self, hunter):
        hunter.sendData(self)
        hunter.modifyMonitor(self.getMonitorPeriod() * 60)
        

# 请求取消监控应答
class Buss0104(base.Buss):
    busstype = [0x01, 0x04]
    name = u"请求取消监控应答"
    dr = {u'成功' : 0x01,
          u'失败' : 0x7f}

    def init(self):
        self.response = 0x01
        
    def separate(self, data):
        self.response = data.get(1)
        
    def assemble(self, data):
        data.set(self.response)
        
    def getResponse(self):
        return base.getKey(self.dr, self.response)
        
    def setResponse(self, value):
        self.response = self.dr[value]
        
    def getList(self):
        return [
                [u'应答类型', self.dr.keys(), self.getResponse, self.setResponse]
                ]
        
    def play(self, hunter):
        hunter.sendData(self)
        hunter.stopMonitor()



# 请求监控应答
class Buss0141(base.GpsBuss):
    busstype = [0x01, 0x41]
    dr = {u'成功' : 0x01,
          u'失败' : 0x7f}
        
    def init(self):
        self.response = 0x01
        base.GpsBuss.init(self)
        
    def separate(self, data):
        self.response = data.get(1)
        base.GpsBuss.separate(self, data)
        
    def assemble(self, data):
        data.set(self.response)
        base.GpsBuss.assemble(self, data)
    
    def getResponse(self):
        return base.getKey(self.dr, self.response)
        
    def setResponse(self, value):
        self.response = self.dr[value]
        
    def getList(self):
        return [
                [u'监控应答', self.dr.keys(), self.getResponse, self.setResponse]
                ] +  base.GpsBuss.getList(self)
        
    def play(self, hunter):
        hunter.startMonitor()


# 撤销监控应答
class Buss0142(base.Buss):
    busstype = [0x01, 0x42]
    
    dr = {u'成功' : 0x01,
          u'失败' : 0x7f}
                    
    def init(self):
        self.response = 0x01
        
    def separate(self, data):
        self.response = data.get(1)
        
    def assemble(self, data):
        data.set(self.response)
    
    def getResponse(self):
        return base.getKey(self.dr, self.response)
        
    def setResponse(self, value):
        self.response = self.dr[value]
        
    def getList(self):
        return [
                [u'应答类型', self.dr.keys(), self.getResponse, self.setResponse]
                ]
        
    def play(self, hunter):
        hunter.stopMonitor()

# 修改监控应答
class Buss0143(base.Buss):
    busstype = [0x01, 0x43]
    dr = {u'成功' : 0x01,
          u'失败' : 0x7f}
    
    def init(self):
        self.response = 0x01
        
    def separate(self, data):
        self.response = data.get(1)
        
    def assemble(self, data):
        data.set(self.response)
    
    def getResponse(self):
        return base.getKey(self.dr, self.response)
        
    def setResponse(self, value):
        self.response = self.dr[value]
        
    def getList(self):
        return [
                [u'应答类型', self.dr.keys(), self.getResponse, self.setResponse]
                ]
        
    def play(self, hunter):
        hunter.startMonitor()
        
# 请求取消监控
class Buss0144(base.Buss):
    busstype = [0x01, 0x44]
    
    dr = {u'监控时间超时' :0x1,
          u'用户终止监控' :0x2,
          u'链路维护时间超时' :0x3,
          u'报警信号终止' :0x4,
          CUSTOM : 0x0}

    def init(self):
        self.reason = 0x00
        
    def separate(self, data):
        self.reason = data.get(1)
        
    def assemble(self, data):
        data.set(self.reason)
    
    def getReason(self):
        return base.getKey(self.dr, self.reason)
        
    def setReason(self, value):
        self.reason = self.dr[value]
        
    def getList(self):
        return [
                [u'终止原因', self.dr.keys(), self.getReason, self.setReason]
                ]
    
    def play(self, hunter):
        hunter.requestStopMonitor()



class Buss0145(base.GpsBuss):
    busstype = [0x01, 0x45]
        
    def init(self):
        self.alarm = [0x01, 0x01]
        self.mtCount = 0x01
        
    def separate(self, data):
        self.alarm = data.get(2)
        self.mtCount = data.get(1)
        base.GpsBuss.separate(self, data)
        
    def assemble(self, data):
        data.set(self.alarm, self.mtCount)
        base.GpsBuss.assemble(self, data)
    
    def getalarmstyle(self):
        pass
    
    def setalarmstyle(self, value):
        pass
    
    def getMtCnt(self):
        pass
    
    def setMtCnt(self, value):
        pass
    
    def getList(self):
        return [
                [u'监控中心数', (0, 255), self.getMtCnt, self.setMtCnt]
                ] + base.GpsBuss.getList(self)
    
    def play(self, hunter):
        pass
    

from twisted.internet import reactor
class Hunter(base.HunterMaster):
    name = u'千里眼监控功能'
    operation = (Buss0101, Buss0102, Buss0103, Buss0104)
    wanted = (Buss0101, Buss0141, 
              Buss0102, Buss0142, 
              Buss0103, Buss0143, 
              Buss0104, Buss0144, 
              Buss0145,)

    def requestMonitor(self, period):
        self.period = period
        
    def modifyMonitor(self, period):
        pass
            
    def startMonitor(self):
        #self.stopCall = reactor.callLater(self.period, self.stopMonitor)
        pass
        
    def requestStopMonitor(self):
        pass
        
    def stopMonitor(self):
        pass
        
        
if __name__ == '__main__':
    from new import classobj as cs
    x = cs('x', (huntermaster.DataTable,), {})
    print x()